package com.ayz.gateway.common.constants;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @ClassName GatewayServiceData
 * @Description TODO: 网关服务的详细信息
 * @Author 快乐的星球
 * @Date 2023/5/25 14:54
 * @Version 1.0
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GatewayServiceData {
    /**
     * 网关服务的唯一标识Id
     * 默认 “gateway@Ip@Port”
     */
    private String gatewayId;
    /**
     * 网关的名称
     */
    private String gatewayName;
    /**
     * 网关提供服务的地址
     */
    private String address;
    /**
     * 网关业务组的名称
     */
    private String gatewayGroup;


    public static String generateServerAddress(String host,int serverPort){
        if(StrUtil.isNotBlank(host)){
            return host+":"+serverPort;
        }
        try {
            return InetAddress.getLocalHost().getHostAddress()+":"+serverPort;
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("获取本机IP地址报错!");
        }
    }
}
